Skip to content

fix(resource/mongodbatlas_advanced_cluster): Emit warning when use_effective_fields and auto-scaling are enabled and spec fields change#4405

Open
marcabreracast wants to merge 10 commits intomasterfrom
CLOUDP-388940_Emit_Warning_When_UseEffectiveFields_Is_Used
Open

fix(resource/mongodbatlas_advanced_cluster): Emit warning when use_effective_fields and auto-scaling are enabled and spec fields change#4405
marcabreracast wants to merge 10 commits intomasterfrom
CLOUDP-388940_Emit_Warning_When_UseEffectiveFields_Is_Used

Conversation

@marcabreracast
Copy link
Copy Markdown
Collaborator

@marcabreracast marcabreracast commented Apr 27, 2026

Description

Emits a Terraform warning during ModifyPlan when all three conditions are met:

  • use_effective_fields = true
  • Auto-scaling is enabled in the config (compute_enabled = true or disk_gb_enabled = true)
  • The user changes the following fields: instance_size, disk_size_gb, or disk_iops

In this combination, the Atlas API silently ignores the spec change, the new values are stored in Terraform state but the cluster is never modified, producing a false "1 changed" result with no feedback. The warning makes this visible and directs the user to temporarily disable auto-scaling to apply the change.

Link to any related issue(s): https://jira.mongodb.org/browse/CLOUDP-388940

Type of change:

  • Bug fix (non-breaking change which fixes an issue). Please, add the "bug" label to the PR.
  • New feature (non-breaking change which adds functionality). Please, add the "enhancement" label to the PR. A migration guide must be created or updated if the new feature will go in a major version.
  • Breaking change (fix or feature that would cause existing functionality to not work as expected). Please, add the "breaking change" label to the PR. A migration guide must be created or updated.
  • This change requires a documentation update
  • Documentation fix/enhancement

Required Checklist:

  • I have signed the MongoDB CLA
  • I have read the contributing guides
  • I have checked that this change does not generate any credentials and that they are NOT accidentally logged anywhere.
  • I have added tests that prove my fix is effective or that my feature works per HashiCorp requirements
  • I have added any necessary documentation (if appropriate)
  • I have run make fix and verified my code
  • If changes include deprecations or removals I have added appropriate changelog entries.
  • If changes include removal or addition of 3rd party GitHub actions, I updated our internal document. Reach out to the APIx Integration slack channel to get access to the internal document.

Testing

The warning itself cannot be asserted programmatically. The Terraform Testing framework has no mechanism to assert warning. The code path is exercised by the existing TestAccAdvancedCluster_effectiveComputeAutoScalingInstanceSize, where if there are any code regressions the issue would be caught. Manual validation has been done in order to ensure the warning surfaces according to the acceptance criteria:

  1. use_effective_fields = true
  2. Any form of auto-scaling is enabled (compute_enabled = true or disk_gb_enabled = true)
  3. A change is detected in instance_size, disk_size_gb, or disk_iops

Baseline config:

resource "mongodbatlas_advanced_cluster" "test" {
  project_id   = var.project_id
  name         = var.cluster_name
  cluster_type = "REPLICASET"

  use_effective_fields = true

  replication_specs = [{
    region_configs = [{
      provider_name = "AWS"
      region_name   = "EU_WEST_1"
      priority      = 7

      electable_specs = {
        node_count    = 3
        instance_size = "M10"
        disk_size_gb  = 10
      }

      auto_scaling = {
        compute_enabled            = true
        compute_scale_down_enabled = true
        compute_min_instance_size  = "M10"
        compute_max_instance_size  = "M40"
        disk_gb_enabled            = false
      }
    }]
  }]

  backup_enabled                 = false
  termination_protection_enabled = false
}
# Scenario Warning message Result
1 Create cluster with instance_size = "M10" and auto-scaling enabled No warning
2 Change instance_size M10 → M20 (auto-scaling on) The change to instance_size will be stored in Terraform state but will not modify the actual cluster in Atlas.
3 Change disk_size_gb 10 → 20 (auto-scaling on) The change to disk_size_gb will be stored in Terraform state but will not modify the actual cluster in Atlas.
4 Change disk_iops 3000 → 4000 (auto-scaling on) The change to disk_iops will be stored in Terraform state but will not modify the actual cluster in Atlas.
5 Disable auto-scaling and change instance_size (auto-scaling off) No warning
6 Disable use_effective_fields and change instance_size M10 → M20 No warning, change applied successfully in Atlas
Full warning message (case 2)
│ Warning: Spec change ignored due to auto-scaling
│ 
│   with mongodbatlas_advanced_cluster.test,
│   on main.tf line 14, in resource "mongodbatlas_advanced_cluster" "test":
│   14: resource "mongodbatlas_advanced_cluster" "test" {
│ 
│ The change to instance_size will be stored in Terraform state but will not
│ modify the actual cluster in Atlas. When use_effective_fields is true and
│ auto-scaling is enabled, Atlas controls instance_size, disk_size_gb, and
│ disk_iops values. To apply this change, temporarily disable auto-scaling.
│ See: https://registry.terraform.io/providers/mongodb/mongodbatlas/latest/docs/resources/advanced_cluster#manually-updating-specs-with-use_effective_fields

Further comments

Copilot AI review requested due to automatic review settings April 27, 2026 14:27
@marcabreracast marcabreracast changed the title fix: emit warning when use effective fields is used feat: emit warning when use effective fields is used Apr 27, 2026
@marcabreracast marcabreracast changed the title feat: emit warning when use effective fields is used fix: Emit warning when use effective fields is used Apr 27, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates the advanced cluster plan modification logic to warn users when use_effective_fields = true is combined with auto-scaling and the configuration changes spec fields that Atlas will ignore.

Changes:

  • Emit a Terraform warning when use_effective_fields=true, auto-scaling is enabled, and instance_size / disk_size_gb / disk_iops change.
  • Refactor autoScalingUsed to accept a single cluster model and adjust auto-scaling keep-unknown detection accordingly.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread internal/service/advancedcluster/plan_modifier.go
Comment on lines 61 to 64
autoScalingFields := determineKeepUnknownsAutoScaling(ctx, diags, state, plan)
keepUnknown = append(keepUnknown, autoScalingFields...)
emitWarningIfSpecChangedWithAutoScaling(ctx, diags, plan, attributeChanges)
schemafunc.CopyUnknowns(ctx, state, plan, keepUnknown, nil)
Copy link

Copilot AI Apr 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR introduces new user-facing behavior (a warning when use_effective_fields=true, auto-scaling is enabled, and spec fields change), but there’s no automated coverage asserting when the warning should/shouldn’t be emitted. Adding a focused unit test around emitWarningIfSpecChangedWithAutoScaling (or exercising it via an existing plan modifier test harness) would help prevent regressions and false positives.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's not a way of asserting warnings into the test framework. The code path is exercised by the existing TestAccAdvancedCluster_effectiveComputeAutoScalingInstanceSize test, which runs the exact scenario. A panic or logic error would be caught there.

Behaviour of the warning is documented and manual tests are done in the PR description.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as discussed offline, consider if it's worth it to add some unit tests

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added in c43d6c8.
Also added a comment on why the duplication of schema attributes to avoid uppercasing in schema.go, feel free to review and if too verbose, I can revert it

@marcabreracast marcabreracast marked this pull request as ready for review April 28, 2026 09:03
@marcabreracast marcabreracast requested review from a team as code owners April 28, 2026 09:04
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Apr 28, 2026

APIx bot: a message has been sent to Docs Slack channel
APIx bot: a message has been sent to Docs Slack channel

@marcabreracast marcabreracast changed the title fix: Emit warning when use effective fields is used fix: mongodbatlas_advanced_cluster emit warning when use effective fields is used with spec fields Apr 28, 2026
@marcabreracast marcabreracast changed the title fix: mongodbatlas_advanced_cluster emit warning when use effective fields is used with spec fields fix: emit warning at plan/apply when effective fields is used along with spec fields Apr 28, 2026
@marcabreracast marcabreracast changed the title fix: emit warning at plan/apply when effective fields is used along with spec fields fix(resource/mongodbatlas_advanced_cluster): emit warning when use_effective_fields and auto-scaling are enabled and spec fields change Apr 28, 2026
@augmentcode
Copy link
Copy Markdown

augmentcode Bot commented Apr 28, 2026

🤖 Augment PR Summary

Summary: This PR adds a plan-time warning for mongodbatlas_advanced_cluster to make it explicit when certain spec updates won’t actually be applied by Atlas.

Changes:

  • Introduces a centralized list of spec fields managed by Atlas under auto-scaling (instance_size, disk_size_gb, disk_iops).
  • Emits a Terraform warning during ModifyPlan when use_effective_fields=true, auto-scaling is enabled, and any of those spec fields are changed.
  • Refactors autoScalingUsed to operate on a single cluster model and reuses the shared field list for both warning + keep-unknown behavior.
  • Adds a changelog entry documenting the new warning behavior.

🤖 Was this summary useful? React with 👍 or 👎

Copy link
Copy Markdown

@augmentcode augmentcode Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review completed. 1 suggestion posted.

Fix All in Augment

Comment augment review to trigger a new review at any time.

Comment thread internal/service/advancedcluster/plan_modifier.go Outdated
@marcabreracast marcabreracast changed the title fix(resource/mongodbatlas_advanced_cluster): emit warning when use_effective_fields and auto-scaling are enabled and spec fields change fix(resource/mongodbatlas_advanced_cluster): Emit warning when use_effective_fields and auto-scaling are enabled and spec fields change Apr 28, 2026
@marcabreracast marcabreracast marked this pull request as draft April 28, 2026 09:13
@marcabreracast marcabreracast marked this pull request as ready for review April 28, 2026 09:19
Copy link
Copy Markdown

@augmentcode augmentcode Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review completed. No suggestions at this time.

Comment augment review to trigger a new review at any time.

Comment thread internal/service/advancedcluster/plan_modifier.go Outdated
// emitWarningIfSpecChangedWithAutoScaling warns when use_effective_fields=true and auto-scaling is enabled but the user
// changed instance_size, disk_size_gb, or disk_iops. Atlas silently ignores these changes in that combination
func emitWarningIfSpecChangedWithAutoScaling(ctx context.Context, diags *diag.Diagnostics, plan *TFModel, attributeChanges schemafunc.AttributeChanges) {
if !plan.UseEffectiveFields.ValueBool() || !autoScalingUsed(ctx, diags, plan) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

q: autoScalingUsed ORs auto_scaling and analytics_auto_scaling, but per Atlas behavior auto_scaling.compute_enabled only governs electable/read_only specs and analytics_auto_scaling.compute_enabled only governs analytics specs (disk is governed by the regular block alone). so auto_scaling.compute_enabled = false + analytics_auto_scaling.compute_enabled = true + change to electable_specs.instance_size -> warning fires, but Atlas honors the change

is this v1 over-approximation acceptable (worth a follow-up ticket), or worth scoping the check per-block before merge?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd go with the approach of trying to scope this check before the merge so I can bring it to the tech sync and align with the team. Working on investigating the possibility of this

Comment on lines +85 to +92
diags.AddWarning(
"Spec change ignored due to auto-scaling",
fmt.Sprintf("The change to %s will be stored in Terraform state but will not modify the actual cluster in Atlas. "+
"When use_effective_fields is true and auto-scaling is enabled, Atlas controls instance_size, disk_size_gb, and disk_iops values. "+
"To apply this change, temporarily disable auto-scaling. "+
"See: https://registry.terraform.io/providers/mongodb/mongodbatlas/latest/docs/resources/advanced_cluster#manually-updating-specs-with-use_effective_fields",
strings.Join(changedFields, ", ")),
)
Copy link
Copy Markdown
Member

@lantoli lantoli Apr 28, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: style drifts a bit from the existing diagnostics in this file (e.g. the read_only_specs AddError at lines 37-42 — specific title naming the trigger, second-person voice, full action sequence). consider matching, e.g.

Suggested change
diags.AddWarning(
"Spec change ignored due to auto-scaling",
fmt.Sprintf("The change to %s will be stored in Terraform state but will not modify the actual cluster in Atlas. "+
"When use_effective_fields is true and auto-scaling is enabled, Atlas controls instance_size, disk_size_gb, and disk_iops values. "+
"To apply this change, temporarily disable auto-scaling. "+
"See: https://registry.terraform.io/providers/mongodb/mongodbatlas/latest/docs/resources/advanced_cluster#manually-updating-specs-with-use_effective_fields",
strings.Join(changedFields, ", ")),
)
diags.AddWarning(
"Spec change ignored when use_effective_fields is true and auto-scaling is enabled",
fmt.Sprintf("Your changes to %s will be stored in Terraform state but will not modify the actual cluster in Atlas. "+
"When use_effective_fields is true and auto-scaling is enabled, Atlas controls instance_size, disk_size_gb, and disk_iops values. "+
"To apply your changes, disable auto-scaling and apply, then re-enable auto-scaling in a separate apply. "+
"See: https://registry.terraform.io/providers/mongodb/mongodbatlas/latest/docs/resources/advanced_cluster#manually-updating-specs-with-use_effective_fields",
strings.Join(changedFields, ", ")),
)

separately: not sure about having the link as it might get outdated. also please check the message looks fine when multiple fields are changed simultaneously.

Copy link
Copy Markdown
Collaborator Author

@marcabreracast marcabreracast Apr 29, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in 80a7e52.

Note that the link is added per the acceptance criteria of the ticket:

Acceptance Criteria
Warning includes the specific field(s) being changed and a link to the documentation

If we remove the link, we'd also have to adjust the acceptance criteria

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i see the risk of the link getting outdated but ok to leave

Comment on lines 61 to 64
autoScalingFields := determineKeepUnknownsAutoScaling(ctx, diags, state, plan)
keepUnknown = append(keepUnknown, autoScalingFields...)
emitWarningIfSpecChangedWithAutoScaling(ctx, diags, plan, attributeChanges)
schemafunc.CopyUnknowns(ctx, state, plan, keepUnknown, nil)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as discussed offline, consider if it's worth it to add some unit tests

Comment thread internal/service/advancedcluster/plan_modifier.go Outdated
Comment thread internal/service/advancedcluster/plan_modifier.go Outdated
Copy link
Copy Markdown
Contributor

@lmkerbey-mdb lmkerbey-mdb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

Copy link
Copy Markdown
Contributor

@lmkerbey-mdb lmkerbey-mdb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

}
}

func buildPlanWithAutoScaling(t *testing.T, useEffectiveFields, computeEnabled, diskGBEnabled bool) *advancedcluster.TFModel {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

consider changing WarnIgnoredSpecChange signature to take useEffectiveFields and autoScalingEnabled bool instead of ctx and plan *TFModel, moving the autoScalingUsed call to the call site in handleModifyPlan.
this way unit tests pass booleans directly with no framework objects to construct

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants